{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1986-01-02</th>\n",
       "      <td>25.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-03</th>\n",
       "      <td>26.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-06</th>\n",
       "      <td>26.53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-07</th>\n",
       "      <td>25.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-01-08</th>\n",
       "      <td>25.87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Price\n",
       "Date             \n",
       "1986-01-02  25.56\n",
       "1986-01-03  26.00\n",
       "1986-01-06  26.53\n",
       "1986-01-07  25.85\n",
       "1986-01-08  25.87"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = '../data/wti-daily.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                parse_dates=['Date'],\n",
    "                index_col=['Date'])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "Use `resample` to find, for each quarter, the mean and standard deviations in price."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">Price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1986-03-31</th>\n",
       "      <td>17.217213</td>\n",
       "      <td>4.856866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-06-30</th>\n",
       "      <td>13.866094</td>\n",
       "      <td>1.346364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-09-30</th>\n",
       "      <td>13.813906</td>\n",
       "      <td>1.809548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1986-12-31</th>\n",
       "      <td>15.406452</td>\n",
       "      <td>0.824353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1987-03-31</th>\n",
       "      <td>18.250328</td>\n",
       "      <td>0.661624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>42.524921</td>\n",
       "      <td>3.844217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-31</th>\n",
       "      <td>58.093443</td>\n",
       "      <td>4.931396</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-06-30</th>\n",
       "      <td>66.186667</td>\n",
       "      <td>4.403607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-09-30</th>\n",
       "      <td>70.575469</td>\n",
       "      <td>3.078296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>77.701273</td>\n",
       "      <td>5.712271</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>144 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Price          \n",
       "                 mean       std\n",
       "Date                           \n",
       "1986-03-31  17.217213  4.856866\n",
       "1986-06-30  13.866094  1.346364\n",
       "1986-09-30  13.813906  1.809548\n",
       "1986-12-31  15.406452  0.824353\n",
       "1987-03-31  18.250328  0.661624\n",
       "...               ...       ...\n",
       "2020-12-31  42.524921  3.844217\n",
       "2021-03-31  58.093443  4.931396\n",
       "2021-06-30  66.186667  4.403607\n",
       "2021-09-30  70.575469  3.078296\n",
       "2021-12-31  77.701273  5.712271\n",
       "\n",
       "[144 rows x 2 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('1Q').agg(['mean', 'std'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "In which quarter did we see the biggest increase in mean price from the previous quarter?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Price    25.91959\n",
       "Name: 2008-06-30 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('1Q').mean().diff().sort_values('Price', ascending=False).iloc[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    " What was the biggest percentage increase in oil prices across quarters?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Price    0.475456\n",
       "Name: 1990-09-30 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    df\n",
    "    .resample('1Q')\n",
    "    .mean()\n",
    "    .pct_change()\n",
    "    .sort_values('Price', ascending=False)\n",
    "    .iloc[0]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
